package collectionDemo;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.TimeUnit;
/**
 * @author Ajie
 * @date 2024/3/6
 * @function
 *
 *
boolean isAdded = deque.offer(element, 2, TimeUnit.SECONDS);// 尝试在2秒内插入元素【队尾】

Integer element = deque.poll(2, TimeUnit.SECONDS); // 尝试在2秒内从队列中取出元素【队首】否则null

 */
public class LinkedBlockQueue_waitTime {


    public static void main(String[] args) throws InterruptedException {
        LinkedBlockingDeque<Integer> deque = new LinkedBlockingDeque<>();

        // 尝试在2秒内插入元素
        boolean isAdded = deque.offer(1, 2, TimeUnit.SECONDS);
        if (isAdded) {
            System.out.println("元素插入成功");
        } else {
            System.out.println("等待超时，元素插入失败");
        }

        // 尝试在2秒内从队列中取出元素
        Integer element = deque.poll(2, TimeUnit.SECONDS);
        if (element != null) {
            System.out.println("取出的元素: " + element);
        } else {
            System.out.println("等待超时，队列仍为空");
        }
    }
}
